<template>
	<view>
	<view class="hm-bg-f hm-p-tb-20">
		<view class="login">
			<view class="title">欢迎登录</view>
			<view v-if="mode =='send' || mode =='key'" class="sed-msg">
				<input :disabled="is_input" class="u-border-bottom" type="number" v-model="tel" placeholder="请输入手机号" />
				<view class="tips ">未注册的手机号会自动完成注册，初始密码为手机号</view>
			</view>
			<view v-if="mode == 'key'" class="key-input">
				<u-code-input v-model="captcha" @finish="checkCaptcha" :focus="true" maxlength="4"></u-code-input>
				<view class="captcha">
					<text :class="{ regain: !show }">{{ second }}秒后重新获取验证码</text>
				</view>
			</view>
			<view v-if="mode =='send' || mode =='key'">
				<view class="hm-p-t-20"  v-if="noCaptcha || mode =='send'">
					<button @click="getCaptcha" :style="[inputStyle]" class="getCaptcha">获取短信验证码</button>
				</view>
			</view>
			<view class="alternative">
				<!--
				<view class="password">密码登录</view>
				<view class="issue">遇到问题</view>
				-->
			</view>
		</view>
	</view>
	<!--技术支持 -->
	<view class="hm">
		{{applet.copyright}} <text class="v">V{{applet.version}}</text>
	</view>
	</view>
</template>

<script>
	const App = getApp();
	export default {
		data() {
			return {
				applet: null,
				mode: 'send',
				is_input: false,
				tel: '',
				captcha:'',
				noCaptcha: true,
				second: 300,
			}
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(opt) {
			let _this = this;
			_this.applet = uni.getStorageSync('applet');
			_this.countdown();
		},
		
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {},
		computed: {
			inputStyle() {
				let style = {};
				if (this.tel) {
					style.color = "#fff";
					style.backgroundColor = "#19be6b";
				}
				return style;
			}
		},
		methods: {
			/**
			 * 验证码登录
			 */
			checkCaptcha(value) {
				let _this = this;
				App._post_form('user/checkPhoneCaptcha', {
					phone: _this.tel,
					captcha: value
				}, function(result) {
					App.showSuccess(result.msg, function() {
						uni.setStorageSync('user',result.data);
						uni.navigateBack();
					});
				}, false, function() {});
			},
			/**
			 * 验证码倒计时
			 */
			countdown(){
				let _this = this;
				let interval = setInterval(() => {
					_this.second--;
					if (_this.second <= 0) {
						_this.mode = 'send';
						_this.tel = '';
						_this.is_input = false;
						_this.noCaptcha = true;
						clearInterval(interval);
					}
				}, 1000);
			},
			/**
			 * 获取验证码
			 */
			getCaptcha() {
				let _this = this;
				if (this.$u.test.mobile(_this.tel)) {
					//请求验证码
					App._post_form('user/sendsms', {
						phone: _this.tel
					}, function(result) {
						App.showSuccess(result.msg, function() {
							_this.mode = 'key';
							_this.is_input = true;
							_this.noCaptcha = false;
							_this.countdown();//验证码倒计时
							
						});
					}, false, function() {});
				} else {
					App.showError('手机号码错误');
					return false;
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.login {
		width: 600rpx;
		margin: 80rpx auto 0;
		font-size: 28rpx;
		.title {
			text-align: left;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 100rpx;
		}
		.tips {
			font-size: 26rpx;
			color: #909193;
			margin-bottom: 60rpx;
			margin-top: 8rpx;
		}
		.sed-msg {
			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}
		}
		.key-input {
			.captcha {
				color: #ff9900;
				margin-top: 20rpx;
				.regain {
					display: block;
				}
			}
		}
		
		.getCaptcha {
			color: #303133;
			background-color: rgb(253, 243, 208);
			border: none;
			font-size: 30rpx;
			padding: 12rpx 0;
			&::after {
				border: none;
			}
		}
		.alternative {
			color: #606266;
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
		}
	}
</style>